package com.add.system;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ACardTable {
  private static final Log _log = LogFactory.getLog(ACardTable.class);
  
  private static ACardTable _instance;
  
  private static final Map<Integer, ACard> _cardIndex = new HashMap<>();
  
  public static ACardTable get() {
    if (_instance == null)
      _instance = new ACardTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int i = 0;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `w_变身卡片能力登入`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int id = rs.getInt("流水号");
        String msg1 = rs.getString("获得能力时的讯息");
        String msg2 = rs.getString("出现显示能力页面名称");
        String cmd = rs.getString("对话档指令");
        int questid = rs.getInt("任务编号");
        int polyid = rs.getInt("变身编号");
        int polytime = rs.getInt("变身时效");
        int polyitemid = rs.getInt("变身消耗道具编号");
        int polyitemcount = rs.getInt("变身消耗道具数量");
        int str = rs.getInt("力量");
        int dex = rs.getInt("敏捷");
        int con = rs.getInt("体质");
        int Int = rs.getInt("智力");
        int wis = rs.getInt("精神");
        int cha = rs.getInt("魅力");
        int ac = rs.getInt("防御");
        int hp = rs.getInt("血量");
        int mp = rs.getInt("魔量");
        int hpr = rs.getInt("回血量");
        int mpr = rs.getInt("回魔量");
        int dmg = rs.getInt("近距离伤害");
        int bowdmg = rs.getInt("远距离伤害");
        int hit = rs.getInt("近距离命中");
        int bowhit = rs.getInt("远战攻击命中");
        int dmgr = rs.getInt("物理减免伤害");
        int mdmgr = rs.getInt("魔法减免伤害");
        int sp = rs.getInt("魔攻");
        int mhit = rs.getInt("魔法命中");
        int mr = rs.getInt("魔法防御");
        int fire = rs.getInt("火属性防御");
        int water = rs.getInt("水属性防御");
        int wind = rs.getInt("风属性防御");
        int earth = rs.getInt("地属性防御");
        ACard card = new ACard(id, msg1, msg2, cmd, questid, polyid, polytime, polyitemid, polyitemcount, 
            str, dex, con, Int, wis, cha, ac, hp, mp, hpr, mpr, dmg, bowdmg, hit, bowhit, dmgr, mdmgr, sp, 
            mhit, mr, fire, water, wind, earth);
        _cardIndex.put(Integer.valueOf(id), card);
        i++;
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("读取->w_变身卡片能力登入: " + i + "(" + timer.get() + "ms)");
  }
  
  public int ACardSize() {
    return _cardIndex.size();
  }
  
  public ACard getCard(int id) {
    return _cardIndex.get(Integer.valueOf(id));
  }
}
